Imports KanBan.Pgd.Infrastructure.Exceptions
Imports KanBan.Pgd.Presentation.Helpers
Imports KanBan.Pgd.Domain.Entities
Imports KanBan.Pgd.Presentation.Controllers
Imports KanBan.Pgd.Presentation.Views
Imports KanBan.Pgd.Infrastructure.Repositories

Public Class EditGroup

    Public Event GroupAdded(ByVal group As Group)
    Public Event GroupUpdated(ByVal group As Group)

    Public Sub New()

        Try

            ' This call is required by the Windows Form Designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.

            Me.FormMode = FormMode.Add
            Me.FormTitle = "Adding New Group"

        Catch ex As Exception

            FormHelper.ShowError(ExceptionHandler.HandleException(ex))

        End Try

    End Sub

    Public Sub New(ByVal groupId As Guid)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Me.FormMode = FormMode.Edit

        Dim groupRepository As New GroupRepository()
        Dim group As Group = groupRepository.GetById(groupId)

        Me.FormTitle = String.Format("Editing Group: {0}", group.Name)

        Me.Id = group.Id
        Me.GroupName = group.Name

    End Sub

#Region "IEditGroupView"

    Public Property GroupName() As String
        Get
            Return txtName.Text
        End Get
        Set(ByVal value As String)
            txtName.Text = value
        End Set
    End Property

    Public Property FormTitle() As String
        Get
            Return txtFormTitle.Text
        End Get
        Set(ByVal value As String)
            txtFormTitle.Text = value
        End Set
    End Property

    Private _formMode As FormMode
    Public Property FormMode() As FormMode
        Get
            Return _formMode
        End Get
        Set(ByVal value As FormMode)
            _formMode = value
        End Set
    End Property

    Public Sub RaiseGroupAddedEvent(ByVal group As Group)
        RaiseEvent GroupAdded(group)
    End Sub

    Public Sub RaiseGroupUpdatedEvent(ByVal group As Group)
        RaiseEvent GroupUpdated(group)
    End Sub

    Private _id As Guid
    Public Property Id() As Guid
        Get
            Return _id
        End Get
        Set(ByVal value As Guid)
            _id = value
        End Set
    End Property

#End Region

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        If Me.FormMode = FormMode.Add Then
            If Not IsFormAddingValid() Then
                Exit Sub
            End If
        End If

        If Me.FormMode = FormMode.Edit Then
            If Not IsFormEditingValid() Then
                Exit Sub
            End If
        End If

        Me.SaveOrUpdateGroup()
        Me.Close()
    End Sub

    Public Sub SaveOrUpdateGroup()

        Dim groupRepository As New GroupRepository()
        Dim group As Group = groupRepository.GetById(Me.Id)

        If group Is Nothing Then
            group = New Group(Me.GroupName)
            Me.RaiseGroupAddedEvent(group)
        Else
            group.Name = Me.GroupName
            Me.RaiseGroupUpdatedEvent(group)
        End If

        groupRepository.SaveOrUpdate(group)

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

#Region "Validation"

    Private Function IsFormAddingValid() As Boolean

        Dim isValid As Boolean = IsNameAddingValid()

        Return isValid

    End Function

    Private Function IsFormEditingValid() As Boolean

        Dim isValid As Boolean = IsNameEditingValid()

        Return isValid

    End Function

    Private Function IsNameAddingValid() As Boolean

        If txtName.Text = String.Empty Then
            errorProvider.SetError(txtName, "You must fill in a value for 'Name'")
            Return False
        ElseIf Not Me.IsGroupNameAvailable(txtName.Text) Then
            errorProvider.SetError(txtName, String.Format("The name: {0} is already in use", txtName.Text))
            Return False
        Else
            errorProvider.SetError(txtName, String.Empty)
            Return True
        End If

    End Function

    Public Function IsGroupNameAvailable(ByVal name As String) As Boolean

        Dim groupRepository As New GroupRepository()
        Dim group As Group = groupRepository.Get("Name", name)

        Return group Is Nothing

    End Function

    Private Function IsNameEditingValid() As Boolean

        If txtName.Text = String.Empty Then
            errorProvider.SetError(txtName, "You must fill in a value for 'Name'")
            Return False
        Else
            errorProvider.SetError(txtName, String.Empty)
            Return True
        End If

    End Function

#End Region

End Class